<?php
ini_set('date.timezone','Asia/Shanghai');//调整时区
session_start();
$robot=$_SESSION['robot'];
$s_time=$_SESSION['s_time'];
$e_time=$_SESSION['e_time'];
$time=$_SESSION['time'];
$table=$_SESSION['table'];
//$robot="ap4,ap4x";
//$s_time="2017-04-07 08:00";
//$e_time="2017-04-07 12:00";
$time=10;

$data=array();

if($time==1){
    $f=floor((strtotime($e_time)-strtotime($s_time))/86400);
    $f=floor((strtotime($e_time)-strtotime($s_time))%86400/3600)+$f*24;
    $temp=$s_time;
    for($i=0;$i<=$f;$i++){
        $date[$temp] =$temp;
        $value[$temp] =0;
        $temp=date('Y-m-d H:i',strtotime("$temp+1hours"));
    }
    $num=13;
}else if($time==10){
    //  $f=floor((strtotime($e_time)-strtotime($s_time))/86400);
    //  $f=floor((strtotime($e_time)-strtotime($s_time))%86400/3600)+$f*24;
    $f=floor((strtotime($e_time)-strtotime($s_time))%86400/600);
    //echo $f."<br/>";
    $temp=$s_time;
    for($i=0;$i<=$f;$i++){
        $date[$temp] =$temp;
        $value[$temp] =0;
        $temp=date('Y-m-d H:i',strtotime("$temp+10minute"));
    }
    $num=15;
}
$array = explode(",",$robot);
$robot_where="hdid = '$array[0]'";
for($index=1;$index<count($array);$index++)
{
    $robot_where=$robot_where."or hdid = '$array[$index]'";
}
//echo $robot_where."WWWWWWWWWWWWWW<br/>";
/*连接数据库*/
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "data";

$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
//按10分钟统计某个活动的文本量
//$sql="SELECT * , COUNT( * ) AS count, SUBSTRING(  `date` , 1, 15 ) AS dateindex FROM msg WHERE mtype =1 and (hdid = '$hdid1'or hdid = '$hdid2')GROUP BY SUBSTRING(  `date` , 1, 15 ) HAVING  SUBSTRING(  `date` , 1, '$num' )='$time' ";
$sql="SELECT * , COUNT( * ) AS count, SUBSTRING(  `date` , 1, '$num' ) AS dateindex FROM msg WHERE mtype =10000 and (".$robot_where.") and( date BETWEEN '$s_time'and '$e_time') GROUP BY SUBSTRING(  `date` , 1, '$num' ) ";
//echo $sql."<br/>";
$result = $conn->query($sql);//查询到的结果集
$data1=array();//放置查询的数据
if ($result->num_rows > 0) {
    // 输出每行数据
    $row = $result->fetch_assoc();
    while($row = $result->fetch_assoc()) {
        if($time==1)
            $temp=date("Y-m-d H:i",strtotime( $row['dateindex'].":00:00"));
        else if($time==10)
            $temp=date("Y-m-d H:i",strtotime( $row['dateindex']."0:00"));
        // $data['count']=$row['count'];//按10分钟统计得到的值
        $date[$temp] =$temp;
        $value[$temp] =$row['count'];
    }
} else{
    echo "没有数据";
}
$data['date']=$date;
$data['value']=$value;
echo json_encode($data);
$conn->close();
?>